function _PTJS_Encoder(args,debug) {
  var encoded_args = {"json_args":toJSONString(args,debug)}
  return encoded_args;
}

/*

    UPDATED BY DMONTAUK NOT TO USE Object.prototype
    Adding the functions to the prototype chain is fundamentally broken.
    For example, if we have frames in our application, the Javascript objects
    in one frame might not get the toJSONString function added (if they don't
    include this file). So trying to encode objects is unsafe, since you don't
    know where they're coming from.

    The functions below, however, are safe - we don't care where the data
    comes from, we can encode it into a string.
    
    json.js
    2007-06-11

    Public Domain - taken from http://json.org/json.js

    This file adds these methods to JavaScript:

        array.toJSONString()
        boolean.toJSONString()
        date.toJSONString()
        number.toJSONString()
        object.toJSONString()
        string.toJSONString()
            These methods produce a JSON text from a JavaScript value.
            It must not contain any cyclical references. Illegal values
            will be excluded.

            The default conversion for dates is to an ISO string. You can
            add a toJSONString method to any date object to get a different
            representation.

*/

/*jslint evil: true */


function toJSONString(thing,debug) {
  try {
    // TODO(dobromirv): Clarify this debugging functionality?
    if(debug) debug(thing)
    switch(typeof(thing)) {
      case "string":
        return String_toJSONString(thing);
      case "number":
        return thing;
      case "boolean": 
        return Boolean_toJSONString(thing);
    }
    // This means we have an object.
    if(thing == null) return 'null';
    // instanceof Array doesn't seem to work across iframes - so adding .length
    if(thing instanceof Array || thing.length) return Array_toJSONString(thing);
    if(thing instanceof Date) return Date_toJSONString(thing);
    return Object_toJSONString(thing);
  } catch(e) {
    try {
    var s = "Unable to serialize '"+thing+"' (type: '" + typeof thing + "').";
    s += " Error was:\n" + e;
    alert(s)
    } catch(e) {
      alert(e)
    }
  }
}
  
function Array_toJSONString(thing) {
  var a = [],     // The array holding the member texts.
      i,          // Loop counter.
      l = thing.length,
      v;          // The value to be stringified.
  // For each value in this array...
  for (i = 0; i < l; i += 1) {
    v = thing[i];
    a.push(toJSONString(v))
  }
  // Join all of the member texts together and wrap them in brackets.
  return '[' + a.join(',') + ']';
};


Boolean_toJSONString = function (thing) {
    return String(thing);
};
  
  
function Date_toJSONString(thing) {

  // Ultimately, this method will be equivalent to the date.toISOString method.
  function f(n) {
    // Format integers to have at least two digits.
    return n < 10 ? '0' + n : n;
  }

  return '"' + thing.getFullYear() + '-' +
          f(thing.getMonth() + 1) + '-' +
          f(thing.getDate()) + 'T' +
          f(thing.getHours()) + ':' +
          f(thing.getMinutes()) + ':' +
          f(thing.getSeconds()) + '"';
};


function Number_toJSONString(thing) {
  // JSON numbers must be finite. Encode non-finite numbers as null.
  return isFinite(thing) ? String(thing) : 'null';
};
  
  
function Object_toJSONString(thing) {
  var a = [],     // The array holding the member texts.
      k,          // The current key.
      v;          // The current value.

  // Iterate through all of the keys in the object, ignoring the proto chain.
  for (k in thing) {
    if (thing.hasOwnProperty(k)) {
      v = thing[k];
      a.push(toJSONString(k) + ":" + toJSONString(v));
    }
  }
  // Join all of the member texts together and wrap them in braces.
  return '{' + a.join(',') + '}';
};

String_toJSONString = function (thing) {// If the string contains no control characters, no quote characters, and no// backslash characters, then we can simply slap some quotes around it.// Otherwise we must also replace the offending characters with safe// sequences.  var m = { '\b': '\\b',            '\t': '\\t',            '\n': '\\n',            '\f': '\\f',            '\r': '\\r',            '"' : '\\"',            '\\': '\\\\'  };  if (/["\\\x00-\x1f]/.test(thing)) {    return '"' + thing.replace(/([\x00-\x1f\\"])/g, function (a, b) {          var c = m[b];          if (c) {              return c;          }          c = b.charCodeAt();          return '\\u00' +              Math.floor(c / 16).toString(16) +              (c % 16).toString(16);      }) + '"';  }  return '"' + thing + '"';};


/*var test = ["This is a test",
            "Do control \t \b \x characters work?",
            "Ouch! \\ backslashes",
            "Other ' quotes? \" wow!"
            ];
for(var i=0;i<test.length;i++) {
  var x = String_toJSONString(test[i]);
  var y = eval(x);
  if(test[i] != y) {
    alert(y + "\nFailed!");
  }
  else {
    alert(y + "\nPassed!")
  }
}
*/


